package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jaya.budan.R;
import com.jaya.budan.bean.VIP_JiLu;
import com.jaya.budan.bean.XYF_JiLu;
import com.jaya.budan.utils.ToolUtils;

import java.util.List;

public class LV_VIP_Jilu_Adapter extends BaseAdapter {

    private static final int TYPE_HEADER = 0;  //代表标题
    private static final int TYPE_ITEM = 1;    //代表项目item
    private List<VIP_JiLu> mlist;
    private LayoutInflater inflater;
    public LV_VIP_Jilu_Adapter(Context context, List<VIP_JiLu> list){
        mlist = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
//        int head = 0;  //标题位置
//        for (VIP_JiLu type : mlist) {
//            int size = mlist.size() + 1;
//            int current = position - head;
//            if (current < size) {
//                //返回对应位置的值
//                if (position == 0) {
//                    return size;
//                } else {
//                    return mlist.get(position - 1);
//                }
//            }
//            head += size;
//        }
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        switch (getItemViewType(position)) {
            //分为两种情况加载item
            case TYPE_HEADER: //加载标题布局
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    convertView = inflater.inflate(R.layout.lv_xyfjilu_item_header, parent, false);
                    viewHolder.tv_xyfjilu_header = convertView.findViewById(R.id.tv_xyfjilu_header);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.tv_xyfjilu_header.setText(mlist.get(position).getTime()+ "");
                break;
            case TYPE_ITEM: //加载数据项目布局
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    convertView = inflater.inflate(R.layout.lv_xyfjilu_item, parent, false);
                    viewHolder.tv_xyfjilu_name = convertView.findViewById(R.id.tv_xyfjilu_name);
                    viewHolder.tv_xyfjilu_time = convertView.findViewById(R.id.tv_xyfjilu_time);
                    viewHolder.tv_xyfjilu_fen = convertView.findViewById(R.id.tv_xyfjilu_fen);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.tv_xyfjilu_name.setText(mlist.get(position).getName() + "");
                String date = ToolUtils.timeStamp2Date(mlist.get(position).getCreate_at() + "", "yyyy-MM-dd HH:mm:ss");
                viewHolder.tv_xyfjilu_time.setText(date);
                viewHolder.tv_xyfjilu_fen.setText("充值" + mlist.get(position).getMoney() + "元");
                viewHolder.tv_xyfjilu_fen.setTextColor(Color.parseColor("#999999"));
                break;
        }
        return convertView;
    }

    /**
     * 获取当前item的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
//        int head = 0;
//        for (VIP_JiLu type : mlist) {
//            int size = mlist.size() + 1;
//            int current = position - head;
//            if (current == 0) {
//                return TYPE_HEADER;
//            }
//            head += size;
//        }
        return TYPE_ITEM;
    }

    private class ViewHolder {
        TextView tv_xyfjilu_name, tv_xyfjilu_time, tv_xyfjilu_fen, tv_xyfjilu_header;
    }
}
